package com.dyson.client.lettuce;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;

/**
 * @author:Dyson
 * @time:2019/9/26 17:34
 */
public class LClient {
    public static void main(String[] args) throws Exception {
        //固定语法格式   redis://[password@]host:port[databaseNumber] ,[]为可选
        RedisClient redisClient = RedisClient.create("redis://root@192.168.237.128:6379");
        //获取连接
        StatefulRedisConnection<String, String> connect = redisClient.connect();
        //获取同步操作方式
        RedisCommands<String, String> sync = connect.sync();
        //清空当前redis
        sync.flushall();
        //清空当前db
        sync.flushdb();
        //存取String
        System.out.println(sync.set("k1", "v1"));
        System.out.println(sync.get("k1"));
        //设置时长
        System.out.println(sync.expire("k1", 5));
        //取时长
        System.out.println(sync.ttl("k1"));
        //存list
        System.out.println(sync.lpush("lKey", "1", "2", "2", "..."));
        //存set
        System.out.println(sync.sadd("sKey", "1", "2", "2", "..."));

        //异步操作方式,
        RedisAsyncCommands<String, String> async = connect.async();
        //使用同上,区别在于异步使用子线程执行,返回结果方式不同,例:
        RedisFuture<String> set = async.set("k2", "v2");
        //阻塞等待结果
        System.out.println(set.get());
        //指定等待时长
        //System.out.println(set.get(3, TimeUnit.SECONDS));
        RedisFuture<String> k2 = async.get("k2");
        System.out.println(k2.get());
    }
}
